<template>
	<view class="specs-select">
		<view class="specs-select-container">
			<view class="row" @click="onPopupShow">
				<view class="lable">已选择：</view>
				<view class="content">{{ selectedSpecKey }}</view>
				<view class="iconfont icon-jiantou"></view>
			</view>
			
			<VanPopup
				:show="popupIsShow"
				:round="true"
				position="bottom"
				z-index="0"
				class="popup">
				<view class="header">
					<image class="cover" mode="aspectFit" :src="curSelectedSpecValue.image"></image>
					<view class="base-info">
						<view class="name">{{ info.store_name }}</view>
						
						<view class="stock">
							<view class="price">
								<text>￥</text>
								{{ curSelectedSpecValue.price }}
							</view>
							库存：{{ curSelectedSpecValue.stock }}
						</view>
					</view>
					<view class="iconfont icon-guanbi" @click="onPopupClose"></view>
				</view>
				<view class="body">
					<scroll-view class="specs-scroll-view" scroll-y="true">
						<view class="specs-list">
							<view
								v-for="(specs, idx) in infoData.productAttr"
								:key="idx"
								class="specs-item">
								<view class="specs-item-title">{{ specs.attr_name }}</view>
								<view class="specs-item-values">
									<view
										v-for="(val, idx2) in specs.attr_value"
										:key="idx2"
										:class="{ on: val.check }"
										@click="onSelectSpec(idx, idx2, val)">
										{{ val.attr }}
									</view>
								</view>
							</view>
						</view>
						<view class="stepper">
							<view class="stepper-lable">数量</view>
							<view class="stepper-field">
								<view
									class="iconfont icon-shangpinshuliang-jian"
									:class="{ disabled: disableMinus }"
									@click="onMinus">
								</view>
								<input v-model="buyNum" @input="onInputBuyNum">
								<view
									class="iconfont icon-shangpinshuliang-jia"
									:class="{ disabled: disablePlus }"
									@click="onPlus">
								</view>
							</view>
						</view>
					</scroll-view>
				</view>
			</VanPopup>
		</view>
	</view>
</template>

<script>
	import VanPopup from '@/wxcomponents/vant/popup/index'
	export default {
		components: {
			VanPopup
		},
		props: {
			info: {
				type: Object,
				default: {}
			}
		},
		data () {
			return {
				popupIsShow: false,
				infoData: this.info,
				selectedSpecArr: [],
				buyNum: 1
			}
		},
		computed: {
			selectedSpecKey () {
				return this.selectedSpecArr.join()
			},
			curSelectedSpecValue () {
				return this.infoData.productValue[this.selectedSpecKey]
			},
			disableMinus () {
				return this.buyNum === 1
			},
			disablePlus () {
				return this.buyNum >= this.curSelectedSpecValue.stock
			}
		},
		created () {
			this._initSpecSelectStatus()
		},
		methods: {
			onPopupClose () {
				this.popupIsShow = false
			},
			onPopupShow () {
				this.popupIsShow = true
			},
			onSelectSpec (idx, idx2, spec) {
				this.infoData['productAttr'][idx]['attr_value'].map((spec) => {
					spec.check = false
					return spec
				})
				this.infoData['productAttr'][idx]['attr_value'][idx2]['check'] = true
				let tempSelectedSpecArr = JSON.parse(JSON.stringify(this.selectedSpecArr))
				tempSelectedSpecArr[idx] = spec.attr
				this.selectedSpecArr = tempSelectedSpecArr
			},
			onMinus () {
				if (this.disableMinus) {
					return false
				}
				this.buyNum--
			},
			onPlus () {
				if (this.disablePlus) {
					return false
				}
				this.buyNum++
			},
			onInputBuyNum (e) {
				const value = e.detail.value
				let rs = ''
				const reg = /^\d*$/
				for (let i = 0; i < value.length; i++) {
					const char = value[i]
					if (reg.test(char)) {
						rs += char
					}
				}
				if (rs) {
					if (rs > this.curSelectedSpecValue.stock) {
						rs = this.curSelectedSpecValue.stock
					}
				} else {
					rs = 1
				}
				this.$nextTick(() => {
					this.buyNum = rs
				})
			},
			_initSpecSelectStatus () {
				let tempSpecArr = []
				for (let attr in this.infoData.productValue) {
					tempSpecArr = attr.split(',')
					break
				}
				this.selectedSpecArr = tempSpecArr
				
				this.infoData.productAttr.map((attr, idx) => {
					attr.attr_value.map((spec) => {
						if (spec.attr === tempSpecArr[idx]) {
							spec.check = true
						}
						return spec
					})
					return attr
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.specs-select {
	background-color: #fff;
	&-container {
		.row {
			display: flex;
			align-items: center;
			font-size: 26rpx;
			color: #82848f;
			height: 80rpx;
			padding: 0 30rpx;
			margin-top: 20rpx;
			.content {
				flex: 1;
				font-size: 28rpx;
				color: #282828;
			}
			.iconfont {
				font-size: 28rpx;
				color: #7a7a7a;
			}
		}
		
		::v-deep .popup {
			height: 800rpx;
			.header {
				position: relative;
				display: flex;
				padding: 0 0 30rpx;
				margin-top: 28rpx;
				
				.base-info {
					display: flex;
					flex-direction: column;
					.name {
						flex: 1;
						width: 500rpx;
						font-size: 32rpx;
						color: #202020;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}
					
					.stock {
						font-size: 24rpx;
						color: #999;
						margin-top: 40rpx;
						.price {
							font-size: 36rpx;
							color: #fc4141;
							text {
								font-size: 24rpx;
							}
						}
					}
				}
				
				.cover {
					width: 150rpx;
					min-width: 150rpx;
					height: 150rpx;
				}
				
				.iconfont {
					position: absolute;
					right: 30rpx;
					top: -4rpx;
					font-size: 34rpx;
					color: #8a8a8a;
				}
			}
			
			.body {
				.specs-scroll-view {
					height: calc(800rpx - 192rpx);
					
					.specs-list {
						.specs-item {
							margin-top: 36rpx;
							&-title {
								font-size: 30rpx;
								color: #999;
								padding: 0 30rpx;
							}
							&-values {
								display: flex;
								flex-wrap: wrap;
								padding: 0 30rpx 0 16rpx;
								view {
									font-size: 26rpx;
									color: #282828;
									padding: 6rpx 32rpx;
									border-radius: 24rpx;
									margin: 20rpx 0 0 14rpx;
									background-color: #f2f2f2;
									border: 2rpx solid #f2f2f2;
									&.on {
										color: #e93323;
										background: #fff4f3;
										border-color: #e93323;
									}
								}
							}
						}
					}
					
					.stepper {
						display: flex;
						align-items: center;
						justify-content: space-between;
						padding: 0 30rpx;
						margin-top: 36rpx;
						&-lable {
							font-size: 30rpx;
							color: #999;
						}
						&-field {
							display: flex;
							align-items: center;
							height: 54rpx;
							
							.iconfont {
								display: flex;
								align-items: center;
								justify-content: center;
								width: 84rpx;
								height: 100%;
								font-size: 24rpx;
								&.disabled {
									color: #dedede;
								}
							}
							
							input {
								width: 84rpx;
								color: #282828;
								font-size: 32rpx;
								line-height: 1.4em;
								text-align: center;
								background: #f2f2f2;
								    
							}
						}
					}
				}
			}
		}
	}
}
</style>

